{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solving a quadratic equation\n",
"\n",
"## Try me\n",
"[](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Extra%20Exercises/Ex2.%20Quadratic%20(SOL).ipynb)[](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FExtra%20Exercises%2FEx2.%20Quadratic%20(SOL).ipynb)\n",
"\n",
"If we have an equation in the form **a**x2 + **b**x + **c** = 0, to solve **x** the following formula is applied:\n",
"
\n",
"\n",
"In Python, recall that multiplication is done with an asterisk \\* while power is indicated by two \\*\\*. For example, 2\\*3 is 6 y 2\\*\\*3 is 8.\n",
"\n",
"The square root can be calculated using ```1/2``` as the right operand of the power operator, for instance ```4**(1/2)``` will equal 2.\n",
"\n",
"To ask a user to enter data, the `input()` function is used. This function admits as a parameter a text that will be shown to the user as an aid to request the input data. For example,\n",
"`text = input(\"Enter a text: \")`\n",
"\n",
"If we want the entered text to be treated as an integer in order to perform mathematical operations, we must convert it using the `int()` function. This is also called **casting**. For example, \n",
"`number = int(input(\"Enter an integer number: \")`\n",
"\n",
"If we want the entered number to have decimals, the conversion must be done using the `float()` function. For example, \n",
"`floating_number = float(input(\"Enter a float number: \")`\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far, with all the previous explanation, **develop the following program**:\n",
"\n",
"A program to calculate the solution to a quadratic equation like ax2 + bx + c = 0. It will ask the user for the values of a, b and c, calculate x (possibly have 2 solutions) and print the result on the screen."
]
},
{
"cell_type": "markdown",
"source": [
"## Solution"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [],
"source": [
"\n",
"a = int(input(\"Enter coefficient a: \"))\n",
"b = int(input(\"Enter coefficient b: \"))\n",
"c = int(input(\"Enter coefficient c: \"))\n",
"\n",
"if a != 0:\n",
" # calculate the discriminant\n",
" d = (b**2) - (4*a*c)\n",
" # find two solutions\n",
" sol1 = (-b-(d)**(1/2))/(2*a)\n",
" sol2 = (-b+(d)**(1/2))/(2*a)\n",
" print(f'The solutions are {sol1} and {sol2}')\n"
]
},
{
"cell_type": "markdown",
"source": [
"## Analysis Questions\n",
"\n",
"1. **Error Handling**: What happens if the user enters a non-numeric value? How could we handle this error?\n",
"\n",
"2. **Input Variable Types**: What type of variables are a, b, c, d? How could we modify the program to make them floating point numbers?\n",
"\n",
"3. **Discriminant**: What is the type of sol1 and sol2 when the discriminant is positive? What is the type of sol1 and sol2 when the discriminant is negative? What is the type of sol1 and sol2 when the discriminant is zero?\n",
"\n",
"4. **Formatted Output**: How does the program manage the output of the solutions? How could we modify the program to print the solutions with only 2 decimal places?\n",
"\n"
],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.4"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"source": [],
"metadata": {
"collapsed": false
}
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}